package cn.mw.cmdb.util;

import cn.mw.cmdb.param.CustomPageParam;
import cn.mw.components.mongodb.page.MWPage;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import java.util.List;

public class CustomPageUtil {

    public static MWPage getCustomPageInfo(CustomPageParam param, List ret){
        MWPage mwPage = param.getPage();
        //如果没有设置总数,说明list的大小为总数
        if(mwPage.getTotal() < 0){
            int count = (int)ret.size();
            mwPage.setTotal(count);
            int start = (int)CMDBTool.getPageStart(mwPage.getCurrent() ,mwPage.getSize());
            int end = start + mwPage.getSize();
            if(end > count){
                end = count;
            }
            ret = ret.subList(start ,end);
        }

        mwPage.setRecords(ret);
        return mwPage;
    }

    public static <T> IPage<T> builderIPage(long pageNum, long pageSize, long total, List<T> ret) {
        Page<T> page = new Page<>();
        // 如果小于等于0，表示使用ret的所有数据作为总数
        if (total <= 0) {
            int count = ret.size();
            int start = (int) CMDBTool.getPageStart(pageNum, pageSize);
            int end = start + (int) pageSize;
            if(end > count){
                end = count;
            }
            ret = ret.subList(start ,end);
            page = new Page<>(pageNum, pageSize, count);
        }
        page.setRecords(ret);

        return page;
    }

}
